import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/home/index.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../views/home/welcome.vue'),
        meta: { title: '首页' }
      },
      // 车辆管理路由
      {
        path: '/vehicles',
        name: 'VehicleList',
        component: () => import('../views/vehicle/index.vue'),
        meta: { title: '车辆列表' }
      },
      {
        path: '/vehicle/detail/:id',
        name: 'VehicleDetail',
        component: () => import('../views/vehicle/detail.vue'),
        meta: { title: '车辆详情' }
      },
      {
        path: '/vehicle-brands',
        name: 'VehicleBrands',
        component: () => import('../views/vehicle/brand.vue'),
        meta: { title: '车辆品牌' }
      },
      {
        path: '/vehicle-series',
        name: 'VehicleSeries',
        component: () => import('../views/vehicle/series.vue'),
        meta: { title: '车辆系列' }
      },
      // 车辆使用管理路由
      {
        path: '/vehicle-usage',
        name: 'VehicleUsage',
        component: () => import('../views/vehicle/usage/index.vue'),
        meta: { title: '车辆使用管理' }
      },
      // 门店管理路由
      {
        path: '/stores',
        name: 'StoreList',
        component: () => import('../views/store/index.vue'),
        meta: { title: '门店列表' }
      },
      // 图片管理路由
      {
        path: '/image-categories',
        name: 'ImageCategories',
        component: () => import('../views/image/categories.vue'),
        meta: { title: '图片分类管理' }
      },
      {
        path: '/image-list/:categoryId',
        name: 'ImageList',
        component: () => import('../views/image/list.vue'),
        meta: { title: '图片列表' }
      },
      {
        path: '/image-collections',
        name: 'ImageCollections',
        component: () => import('../views/image/collections.vue'),
        meta: { title: '图片集管理' }
      },
      {
        path: '/image-collection-detail/:collectionId',
        name: 'ImageCollectionDetail',
        component: () => import('../views/image/collection-detail.vue'),
        meta: { title: '图片集详情' }
      },
      // 客服管理路由
      {
        path: '/service',
        component: () => import('../views/layout/service-layout.vue'),
        redirect: '/service/index',
        name: 'Service',
        meta: { title: '客服管理', icon: 'el-icon-service' },
        children: [
          {
            path: 'index',
            name: 'ServiceIndex',
            component: () => import('../views/service/index.vue'),
            meta: { title: '在线客服' }
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.localStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router 